using System.Collections.Generic;
using System.IO;
using System.Linq;
using BearUtil;
using BearUtil.Hidden;
using Sirenix.Utilities;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 拓展辅助工具，用于全局替换数据
/// </summary>
public class HelperTool
{
    [MenuItem("Tools/HelperTool/CheckEmoJiLost")]
    public static void CheckAllEmojiNode()
    {
        var path = "Assets/StoryEditor/Graph/Data/";

        var files = Directory
            .GetFiles(Application.dataPath.Replace("Assets", path), "**.asset", SearchOption.AllDirectories).ToList();

        var assets = new List<StoryGraph>();

        for (var i = files.Count - 1; i >= 0; i--)
        {
            assets.Add(AssetDatabase.LoadAssetAtPath<StoryGraph>(files[i].Replace(Application.dataPath, "Assets")));
        }
        
        // var testPath = "Assets/StoryEditor/Nodes/Dialogs/Data/小美人鱼前传1/小美人鱼前传1_DialogNode_23_graph.asset";
        // var graph = AssetDatabase.LoadAssetAtPath<DialogGraph>(testPath);
        // var dnodes = graph.nodes;
        // for (var j = 0; j < dnodes.Count; j++)
        // {
        //     var bNode = dnodes[j] as BearNode;
        //     if (bNode == null)
        //     {
        //         Debug.LogError("节点丢失： " + graph.name);
        //         continue;
        //     }
        //
        //     if (bNode.GetType().Name.Equals("ShowEmoji"))
        //     {
        //         var emoji = bNode as ShowEmoji;
        //         if (string.IsNullOrEmpty(emoji.expression_name))
        //         {
        //             Debug.LogError("IsLost---" + graph.name + " index: ");
        //         }
        //         else
        //         {
        //             Debug.Log(emoji.expression_name);
        //         }
        //         
        //         EditorUtility.SetDirty(emoji);
        //         AssetDatabase.SaveAssets();
        //     }
        // }
        
        for (var i = 0; i < assets.Count; i++)
        {
            var story = assets[i];
            if (story == null || story.nodes.Count <= 0)
                continue;
        
            foreach (var node in story.nodes)
            {
                if (node.GetType().InheritsFrom(typeof(BearDialog)))
                {
                    var dialog = node as BearDialog;
                    Debug.Log("Check: " + dialog.name + " index: " + dialog.index);
        
                    var dnodes = dialog.graph.nodes;
                    for (var j = 0; j < dnodes.Count; j++)
                    {
                        var bNode = dnodes[j] as BearNode;
                        if (bNode == null)
                        {
                            Debug.LogError("节点丢失： " + dialog.name);
                            continue;
                        }
        
                        if (bNode.GetType().Name.Equals("ShowEmoji"))
                        {
                            var emoji = bNode as ShowEmoji;
                            
                            // 因为数据没更新，现在直接更新数据
                            EditorUtility.SetDirty(emoji);
                            AssetDatabase.SaveAssets();
                        }
                    }
                }
            }
        }
    }

    /// <summary>
    /// ActorsMove 数据赋值
    /// </summary>
    public static void ActorMoveNodesTransform()
    {
        var series = StoriesSetting.Instance.Series;
        for (var i = 0; i < series.Count; i++)
        {
            var handle = series[i];
            var stories = handle.Stories;
            for (int j = 0; j < stories.Count; j++)
            {
                var story = stories[j];
                for (var m = 0; m < story.nodes.Count; m++)
                {
                    var node = story.nodes[m] as BearDialog;
                    if (node != null)
                    {
                        var g = node.selfGraph;

                        if (g == null)
                        {
                            Debug.LogError("Dialogue Lost Graph: " + node.name);
                            return;
                        }

                        for (var n = 0; n < g.nodes.Count; n++)
                        {
                            var bn1 = g.nodes[n] as ActorMove;
                            var bn2 = g.nodes[n] as ScenesLines;
                            var bn3 = g.nodes[n] as ShowEmoji;
                            if (bn1 != null)
                            {
                                bn1._actor = NodeMethodUtil.FindActorByID(bn1.role_id_move);
                            }

                            if (bn2 != null)
                            {
                                bn2._actor = NodeMethodUtil.FindActorByID(bn2.role_id);
                            }
                            
                            if (bn3 != null)
                            {
                                bn3._actor = NodeMethodUtil.FindActorByID(bn3.map_item_id);
                            }
                        }
                    }
                }
            }
        }
    }
    
    public static List<DialogGraph> FindOrphan()
    {
        List<DialogGraph> Orphans = new List<DialogGraph>();
        
        var DialogPath = "Assets/StoryEditor/Nodes/Dialogs/Data";
        var absPath = DialogPath.Replace("Assets", Application.dataPath);
        var objects = Directory.GetFiles(absPath, "*.asset", SearchOption.AllDirectories).ToArray();
        for (var i = 0; i < objects.Length; i++)
        {
            var obj = objects[i];
            var path = obj.Replace(Application.dataPath, "Assets");
            var graph =  AssetDatabase.LoadAssetAtPath<DialogGraph>(path);
            if (graph != null && graph.owner == null)
                Orphans.Add(graph);
        }

        return Orphans;
    }
}